package com.dhcc.bpm.modules.alarm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.modules.alarm.entity.AlarmNotice;
import com.dhcc.bpm.modules.alarm.service.IAlarmNoticeService;
import com.dhcc.bpm.modules.alarm.vo.AlarmNoticeVO;
import com.dhcc.bpm.modules.api.service.IApiService;
import com.dhcc.bpm.modules.api.vo.ApiListVo;
import com.dhcc.bpm.modules.gateway.entity.Gateway;
import com.dhcc.bpm.modules.gateway.service.IGatewayService;
import com.dhcc.bpm.modules.system.entity.User;
import com.dhcc.bpm.modules.system.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 告警通知表 控制器
 *
 * @author ccab
 * @since 2020-07-06
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-alarm/alarmnotice")
@Api(value = "告警通知表", tags = "告警通知表接口")
public class AlarmNoticeController extends BladeController {

    private IAlarmNoticeService alarmNoticeService;

    private IGatewayService gatewayService;

    private IApiService apiService;

    private IUserService userService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入alarmNotice")
    public R<AlarmNotice> detail(AlarmNoticeVO alarmNotice) {
        AlarmNotice detail = alarmNoticeService.getOne(Condition.getQueryWrapper(alarmNotice));
        return R.data(detail);
    }

    /**
     * 分页 告警通知表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入alarmNotice")
    public R<IPage<AlarmNoticeVO>> list(AlarmNoticeVO alarmNotice, Query query) {

        //模糊搜索转换搜索条件
        //警告对象名字转换为警告对象id
        if (alarmNotice.getNoticeUsername() != null && !"".equals(alarmNotice.getNoticeUsername())) {
            User user = new User();
            user.setName(alarmNotice.getNoticeUsername());
            User detail = userService.getOne(Condition.getQueryWrapper(user));
            if (detail != null) {
                alarmNotice.setNoticeUser(detail.getId().toString());
            }
        }
        //网关名称转换为网关编码
        if (alarmNotice.getGatewayName() != null) {
            Gateway gateway = new Gateway();
            gateway.setGatewayName(alarmNotice.getGatewayName());
            Gateway detail = gatewayService.getOne(Condition.getQueryWrapper(gateway));
            if (detail != null) {
                alarmNotice.setGatewayCode(detail.getGatewayCode());
            }
        }

        IPage<AlarmNotice> pages = alarmNoticeService.page(Condition.getPage(query), Condition.getQueryWrapper(alarmNotice));

        IPage<AlarmNoticeVO> page = alarmNoticeService.selectAlarmNoticePage(Condition.getPage(query), new AlarmNoticeVO());
        List<AlarmNoticeVO> list = new ArrayList<AlarmNoticeVO>();
        if (pages.getRecords() != null && pages.getRecords().size() > 0) {
            for (AlarmNotice alarmNotice1 : pages.getRecords()) {
                AlarmNoticeVO vo = new AlarmNoticeVO();
                BeanUtil.copyProperties(alarmNotice1, vo);

                vo.setGatewayName(gatewayService.selectGatewayNameByCode(vo.getGatewayCode()));
                ApiListVo apiListVo = apiService.selectByApiCode(vo.getApiCode());
                if (apiListVo != null) {
                    vo.setApiName(apiListVo.getApiName());
                }
                User user = new User();
                user.setId(Long.parseLong(vo.getNoticeUser()));

                User detail = userService.getOne(Condition.getQueryWrapper(user));
                if (detail != null) {
                    vo.setNoticeUsername(detail.getName());
                }
                list.add(vo);
            }

            page.setRecords(list);
            page.setCurrent(pages.getCurrent());
            page.setSize(pages.getSize());
            page.setTotal(pages.getTotal());
            page.setPages(pages.getPages());
        }

        return R.data(page);
    }

    /**
     * 自定义分页 告警通知表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入alarmNotice")
    public R<IPage<AlarmNoticeVO>> page(AlarmNoticeVO alarmNotice, Query query) {
        IPage<AlarmNoticeVO> pages = alarmNoticeService.selectAlarmNoticePage(Condition.getPage(query), alarmNotice);
        return R.data(pages);
    }

    /**
     * 新增 告警通知表
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入alarmNotice")
    public R save(@Valid @RequestBody AlarmNoticeVO alarmNotice) {
        return R.status(alarmNoticeService.save(alarmNotice));
    }

    /**
     * 修改 告警通知表
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入alarmNotice")
    public R update(@Valid @RequestBody AlarmNoticeVO alarmNotice) {
        return R.status(alarmNoticeService.updateById(alarmNotice));
    }

    /**
     * 新增或修改 告警通知表
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入alarmNotice")
    public R submit(@Valid @RequestBody AlarmNoticeVO alarmNotice) {
        return R.status(alarmNoticeService.saveOrUpdate(alarmNotice));
    }


    /**
     * 删除 告警通知表
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(alarmNoticeService.deleteLogic(Func.toLongList(ids)));
    }


}
